﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using WatiN.Core.Exceptions;

using WindowsFormsApplication4;
using WatiN.Core;

namespace WindowsFormsApplication4
{
    public partial class Form1 : System.Windows.Forms.Form
    {
        public string urlsfilename="";
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFiledDialog = new OpenFileDialog();
            openFiledDialog.InitialDirectory = "D:\\";
            openFiledDialog.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
            openFiledDialog.FilterIndex = 1;
            if (openFiledDialog.ShowDialog() == DialogResult.OK)
            {
                string fname = openFiledDialog.FileName;
                this.textBox1.Text+=fname;
                this.urlsfilename = fname;
                StreamReader sr = File.OpenText(fname);
                string str = sr.ReadLine();
                int i=0;
                while (str != null)
                {
                    
                    this.treeView1.LabelEdit = true;
                    TreeNode tn = new TreeNode();
                    tn.Text = str;
                    i++;
                    treeView1.Nodes.Add(tn);
                    str = sr.ReadLine();

                }
                sr.Close();


            }

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void treeView1_AfterSelect_1(object sender, TreeViewEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            String s = "http://www.baidu.com";
            foreach (TreeNode tn in this.treeView1.Nodes)
            {
                if (tn.Text==s)
                {
                    BaiduTest bt = new BaiduTest();
                    bt.sou();
                }
            }
        }
        //public static GetCaptcha gc = new GetCaptcha();
        private void button3_Click(object sender, EventArgs e)
        {
            StreamReader sr = File.OpenText(urlsfilename);
            string str = sr.ReadLine();
            
            FileStream fs = new FileStream("c:\\fs.txt", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            foreach (TreeNode tn in this.treeView1.Nodes)
            {
                PmTest pt1 = new PmTest();
                if( str != null)
                {
                    try
                    {
                        IE ie = new IE();
                        pt1.test(str, ie);
                        ie.Close();
                        
                    }
                    catch (ElementExceptionBase a)
                    {
                        
                    }
                    sw.WriteLine(pt1.result);
                    str = sr.ReadLine();
                    }
            }
            
            sr.Close();
            sw.Flush();
            sw.Close();
            //fs.Flush();
            fs.Close();
            //string url="";
            //PmTest pt = new PmTest();
            //pt.test(url);
            MessageBox.Show("Done!");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            PmTest pt = new PmTest();
            //IE ie = new IE();
            pt.test1();

        }

        
    }
}
